package com.greenant.store.activity;

import java.text.DecimalFormat;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.SaveCallback;
import com.greenant.store.R;
import com.greenant.store.bean.IncomeInfo;
import com.greenant.store.bean.WithdrawRecord;
import com.greenant.store.utils.Const;
import com.greenant.store.utils.UtilTools;
import com.lsarah.utils.SystemBarTintManager;

public class WithdrawalsApplyActivity extends BaseActivity {
	private TextView mTvAccountMoney;
	private TextView mTvCommit;
	private EditText mEtTixianMoney;
	private View mViewCommit;
	
	private IncomeInfo mIncomeInfo;
	
	private DecimalFormat mDecimalFormat;
	private boolean isCommiting = false;
	
	private TextWatcher mTextWatcher = new TextWatcher() {
		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			try{
				double priceSell = Double.valueOf(s.toString());
				if((mIncomeInfo != null && priceSell <= mIncomeInfo.getAccountBalance()) 
						&& priceSell >= 100 && priceSell % 100 == 0) {
					mTvCommit.setTextColor(getResources().getColor(R.color.firebrick));
				} else {
					mTvCommit.setTextColor(getResources().getColor(android.R.color.darker_gray));
				}
			}
			catch(Exception e) {
				mTvCommit.setTextColor(getResources().getColor(android.R.color.darker_gray));
				e.printStackTrace();
			}
		}
		
		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}
		
		@Override
		public void afterTextChanged(Editable s) {
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		SystemBarTintManager.setContentView(this, R.layout.withdrawals_apply_activity,
				getResources().getColor(R.color.firebrick));
		
		mDecimalFormat = new DecimalFormat("#########.##");
		
		mTvAccountMoney = (TextView)findViewById(R.id.withdrawals_apply_account_money);
		mTvCommit = (TextView)findViewById(R.id.withdrawals_apply_ensure_commit_tv);
		mEtTixianMoney = (EditText)findViewById(R.id.withdrawals_apply_tixian_money);
		mViewCommit = findViewById(R.id.withdrawals_apply_ensure_commit);
		
		mEtTixianMoney.addTextChangedListener(mTextWatcher);
		
		Log.d("CEXX", "CEXX-WithdrawalsApplyActivity-Const.mMyIncomeActivityIncomeInfo:" + Const.mMyIncomeActivityIncomeInfo);
		if(Const.mMyIncomeActivityIncomeInfo != null) {
			mIncomeInfo = Const.mMyIncomeActivityIncomeInfo;
			String keTixianStr = mDecimalFormat.format(mIncomeInfo.getAccountBalance());
			Log.d("CEXX", "CEXX-WithdrawalsApplyActivity-keTixianStr:" + keTixianStr);
			mTvAccountMoney.setText(keTixianStr);
		}
	}
	
	public void goBack(View view) {
		finish();
	}
	
	public void ensureCommit(View view) {
		Editable editable = mEtTixianMoney.getText();
		if(editable != null) {
			try{
				double priceSell = Double.valueOf(editable.toString());
				if(priceSell >= 100 && priceSell % 100 == 0) {
					commitWithdrawRecord(priceSell);
				} else {
					UtilTools.showMyToast(WithdrawalsApplyActivity.this, 
							getResources().getString(R.string.withdrawals_apply_commit_prompt));
				}
			}
			catch(Exception e) {
				UtilTools.showMyToast(WithdrawalsApplyActivity.this, 
						getResources().getString(R.string.withdrawals_apply_commit_prompt));
				e.printStackTrace();
			}
		}
	}
	
	private void commitWithdrawRecord(final double money) {
		if(AVUser.getCurrentUser() == null) {
			return;
		}
		
		if(mIncomeInfo == null || money > mIncomeInfo.getAccountBalance()) {
			UtilTools.showMyToast(WithdrawalsApplyActivity.this, 
					getResources().getString(R.string.withdrawals_apply_commit_prompt_1));
			return;
		}
		
		if(isCommiting) {
			return;
		}
		
		isCommiting = true;
		WithdrawRecord request = new WithdrawRecord();
		request.setUserId(AVUser.getCurrentUser().getObjectId());
		request.setUserName(AVUser.getCurrentUser().getUsername());
		request.setWithdraw(money);
		// 0-等待处理  1-处理中 2-已完成
		request.setStatus(0);
		request.saveInBackground(new SaveCallback() {
			@Override
			public void done(AVException arg0) {
				isCommiting = false;
				if(arg0 == null) {
					updateIncomeInfo(money);
					UtilTools.showMyToast(WithdrawalsApplyActivity.this, 
							getResources().getString(R.string.withdrawals_apply_commit_suc));
					setResult(RESULT_OK);
					finish();
				} else {
					Log.d("CEXX", "CEXX-commitWithdrawRecord-arg0:" + arg0);
					UtilTools.showMyToast(WithdrawalsApplyActivity.this, 
							getResources().getString(R.string.withdrawals_apply_commit_fai));
				}
			}
		});
	}
	
	private void updateIncomeInfo(double money) {
		if(mIncomeInfo != null) {
			mIncomeInfo.setAccountBalance(mIncomeInfo.getAccountBalance() - money);
			mIncomeInfo.setWithdrawal(mIncomeInfo.getWithdrawal() + money);
			mIncomeInfo.saveInBackground(new SaveCallback() {
				@Override
				public void done(AVException arg0) {
					Log.d("CEXX", "CEXX-updateIncomeInfo-arg0:" + arg0);
				}
			});
		}
	}
}
